layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form;

    //用户管理
    table.render({
        elem: '#LAY-user-back-manage'
        , url: layui.setter.html + '/api/users' //模拟接口
        , cols: [[
            {field: 'id', title: 'id'}
            , {field: 'username', title: '用户名'}
            , {field: 'ctime', title: '创建时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-user-update-password'}
        ]]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-user-back-manage)', function (obj) {
        var data = obj.data;
        var name = data.username;
        if (obj.event === 'edit') {
            var tr = $(obj.tr);

            layer.open({
                type: 2
                , title: '修改密码'
                , content: '../../../views/home/user/updatepassword.html'
                , area: ['420px', '420px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-back-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(LAY-user-front-submit)', function (data) {
                        var field = data.field; //获取提交的字段
                        //确认密码
                        if (field.password !== field.repass) {
                            return layer.msg('两次密码输入不一致');
                        }
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: "/api/user",
                            type: "PUT",
                            dataType: "JSON",
                            data: JSON.stringify(field),
                            contentType: 'application/json',
                            success: function (obj) {
                                layer.msg("密码修改成功");
                                table.reload('LAY-user-back-manage'); //数据刷新
                                layer.close(index); //关闭弹层
                            },
                            error: function (obj) {
                                layer.msg(obj.responseJSON.message)
                            }
                        });
                        table.reload('LAY-user-front-submit'); //数据刷新
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['username']]; //得到iframe页的窗口对象，执行iframe页的方法:iframeWin.method();
                    //console.log(body.html()) //得到iframe页的body内容
                    body.find('#username').val(name)
                }
            })
        }
    });

    exports('useradmin', {})
});